package son.hut.vn;

import java.util.List;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class PlaceSearch extends FragmentActivity{
	private GoogleMap googleMap;
	private EditText editText;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.placesearch);
		
		editText = (EditText)findViewById(R.id.editSearch);
		googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapSearch)).getMap();
		
	}
	
	public void placeSearch(View view){
		//buoc1: lay dau vao cua nguoi dung va kiem tra du lieu
		String place = editText.getText().toString().trim();
		
		//buoc2: lay danh sach cac Address tra ve
		List<Address> arrayAddresses = null;
		Geocoder geocoder = new Geocoder(PlaceSearch.this);
		
		try {
			arrayAddresses = geocoder.getFromLocationName(place, 3);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		//buoc3: lay danh sach cac diem theo danh sach Address
		if(arrayAddresses==null||arrayAddresses.size()==0){
			Toast.makeText(PlaceSearch.this, "No location found!", Toast.LENGTH_LONG).show();
		}else{
			//xoa het sach ban do
			googleMap.clear();
			
			for (int i = 0; i < arrayAddresses.size(); i++) {
				LatLng latLng = new LatLng(arrayAddresses.get(i).getLatitude(), arrayAddresses.get(i).getLongitude());
				
				googleMap.addMarker(new MarkerOptions().position(latLng).title("jfhdkasj").icon(BitmapDescriptorFactory.defaultMarker()));
				
				if(i==0){
					googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
				}
				
			}
		}
		
	}
	
	

}
